package hsp.admin.app.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import hsp.admin.api.ApiResult;
import hsp.admin.app.system.po.CodePO;
import hsp.admin.app.system.service.ICodeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "CodeController", description = "码表管理")
@RestController
@RequestMapping("system/code")
public class CodeController {

    private final ICodeService service;

    @Autowired
    public CodeController(ICodeService service){
        this.service = service;
    }

    @Operation(summary = "获取码表")
    @GetMapping("{id}")
    public ApiResult<CodePO> getCode(@PathVariable("id") long id){
        CodePO po = service.getById(id);
        return ApiResult.success(po);
    }

    @Operation(summary = "根据码组主键获取码表列表")
    @GetMapping("list/{dictId}")
    public ApiResult<List<CodePO>> getCodeListByDictId(@PathVariable String dictId){
        LambdaQueryWrapper<CodePO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CodePO::getDictId, dictId);
        wrapper.orderByAsc(CodePO::getOrdIdx);
        List<CodePO> list = service.list(wrapper);
        return ApiResult.success(list);
    }

    @Operation(summary = "编辑码表")
    @PostMapping()
    public ApiResult<?> postOrPutCode(@RequestBody CodePO po){
        service.saveOrUpdate(po);
        return ApiResult.success();
    }

    @Operation(summary = "删除码表")
    @DeleteMapping("{id}")
    public ApiResult<?> deleteCode(@PathVariable("id") long id){
        service.removeById(id);
        return ApiResult.success();
    }

}
